<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加路由信息</title>
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/1.12.2/jquery.min.js"></script>
    <script type="text/javascript">
        //先用cdn，如未加载到jquery再加载项目下的
        if (typeof jQuery == "undefined") {
            var js = "/js/jquery.min.js";
            document.write('<script type="text/javascript" src="' + js + '"><\/script>');
        }
    </script>
</head>
<body>

<h1>添加路由信息</h1>
<div>
    <div>
        routeId：<input type="text" id="routeId" th:value="${route.routeId}"/>
    </div>
    <br/>
    <div>
        routeUri：<input type="text" id="routeUri" th:value="${route.routeUri}"/>
    </div>
    <br/>
    <div>
        routeOrder：<input type="text" id="routeOrder" th:value="${route.routeOrder}"/>
    </div>
    <br/>
    <div>
        predicates：<textarea id="predicates" rows="4" cols="50" th:text="${route.predicates}"></textarea>
    </div>
    <br/>
    <div>
        filters：<textarea id="filters" rows="4" cols="50" th:text="${route.filters}"></textarea>
    </div>
    <br/>
    <div>
        <input type="button" onclick="window.history.back()" value="返回"/>
        <input type="button" onclick="save()" value="提交"/>
    </div>
</div>
<script type="text/javascript" th:inline="javascript">

    var id = 0;
    var route = [[${route}]];
    console.log(route);
    $(function () {
        init();
    })

    function init() {
        if (route && route.id) {
            console.log("init");
            id = route.id;
        }
    }

    function save() {
        var routeId = getById("routeId").value;
        var routeUri = getById("routeUri").value;
        var routeOrder = getById("routeOrder").value;
        var predicates = getById("predicates").value;
        var filters = getById("filters").value;
        var route = {
            routeId: routeId,
            routeUri: routeUri,
            routeOrder: routeOrder,
            predicates: predicates,
            filters: filters
        };
        var url = "/gateway-routes/add";
        if (id > 0) {
            route.id = id;
            url = "/gateway-routes/edit";
        }
        $.ajax({
            type: "post",
            url: url,
            data: JSON.stringify(route),
            contentType: "application/json",
            success: function (result) {
                if (result && result == "success") {
                    alert("保存成功");
                    window.location.href = '/gateway-routes/list';
                }
            }
        })
    }

    function getById(id) {
        return document.getElementById(id);
    }
</script>
</body>
</html>